import { NextResponse } from "next/server";
import { saveToFiles } from "./tool";


export async function POST(request: Request, requestParams: { params: any }) {
  // get query
  const { searchParams } = new URL(request.url);
  const allSearchParams = Object.fromEntries(searchParams);
  // get params
  // get formdata
  // const formdata = await request.formData();
  // const formdataJson = Object.fromEntries(formdata);
  // get json 如果是 formdata 模式下不要请求
  const json = await request.json();
  console.log(allSearchParams, 'allSearchParams')
  const res = await saveToFiles(json, allSearchParams.unit);
  return NextResponse.json({
    res,
    // allSearchParams,
    // formdataJson,
    // json,
  });
}

export async function GET(request: Request, requestParams: { params: any }) {
  // get query
  const { searchParams } = new URL(request.url);
  const allSearchParams = Object.fromEntries(searchParams);
  // get params
  // get formdata
  // const formdata = await request.formData();
  // const formdataJson = Object.fromEntries(formdata);
  // get json 如果是 formdata 模式下不要请求
  // const json = await request.formData();
  console.log( allSearchParams)

  return NextResponse.json({
    allSearchParams,
    // formdataJson,
    // json,
  });
}
